package com.sskj.wallet.ui.activity;


import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.allen.library.SuperTextView;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.CoinEnum;
import com.sskj.lib.bean.WalletBean;
import com.sskj.lib.model.room.WalletBeanViewModel;
import com.sskj.lib.util.TipUtil;
import com.sskj.wallet.R;
import com.sskj.wallet.R2;
import com.sskj.wallet.bean.WalletCardBean;
import com.sskj.wallet.component.DaggerUserDataComponent;
import com.sskj.wallet.presenter.WalletEditActivityPresenter;
import com.sskj.wallet.uitl.TipWalletUtil;

import java.util.ArrayList;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

@Route(path = RConfig.WALLET_WALLET_EDIT)//钱包管理编辑
public class WalletEditActivity extends BaseActivity<WalletEditActivityPresenter> {
    @Autowired
    String code;
    @BindView(R2.id.etName)
    EditText etName;
    @BindView(R2.id.ivDelete)
    ImageView ivDelete;
    @BindView(R2.id.stvUpdatePwd)
    SuperTextView stvUpdatePwd;
    @BindView(R2.id.stvExportHelpWord)
    SuperTextView stvExportHelpWord;
    @BindView(R2.id.stvExportPrivateKey)
    SuperTextView stvExportPrivateKey;
    @BindView(R2.id.stvExportKeystore)
    SuperTextView stvExportKeystore;
    @BindView(R2.id.btSubmit)
    Button btSubmit;
    @Inject
    WalletBeanViewModel walletBeanViewModel;
    private WalletBean walletBeanBtc;
    private WalletBean walletBeanEth;
    private WalletBean walletBean;

    @Override
    protected int getLayoutId() {
        return R.layout.wallet_activity_wallet_edit;
    }

    @Override
    public WalletEditActivityPresenter getPresenter() {
        return new WalletEditActivityPresenter();
    }

    @Override
    protected void initView() {
        DaggerUserDataComponent.create().inject(this);
        ARouter.getInstance().inject(this);
        ClickUtil.click(stvUpdatePwd, () -> {
            TipUtil.showPwd(this, str -> {
                if (str[0].equals(walletBean.getPwd())) {
                    ARouter.getInstance().build(RConfig.WALLET_UPDATE_PWD)
                            .withString(Constans.CODE, code)
                            .navigation();
                } else {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.wallet_transferActivity5_j));
                }
            });
        });
        ClickUtil.click(stvExportHelpWord, () -> {
            TipUtil.showPwd(this, str -> {
                if (str[0].equals(walletBean.getPwd())) {
                    ARouter.getInstance().build(RConfig.WALLET_BACKUP_TIP)
                            .withBoolean(Constans.IS_PRIVATE, false)
                            .withString(Constans.CODE, code)
                            .navigation();
                } else {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.wallet_transferActivity5_j));
                }
            });


        });
        ClickUtil.click(stvExportPrivateKey, () -> {
            TipUtil.showPwd(this, str -> {
                if (str[0].equals(walletBean.getPwd())) {
                    ARouter.getInstance().build(RConfig.WALLET_BACKUP_TIP)
                            .withBoolean(Constans.IS_PRIVATE, true)
                            .withString(Constans.CODE, code)
                            .navigation();
                } else {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.wallet_transferActivity5_j));
                }
            });
        });
        ClickUtil.click(stvExportKeystore, () -> {
            TipUtil.showPwd(this, str -> {
                if (str[0].equals(walletBean.getPwd())) {
                    ARouter.getInstance().build(RConfig.WALLET_BACKUP_TIP)
                            .withBoolean(Constans.IS_KEYSTORE, true)
                            .withString(Constans.CODE, code)
                            .navigation();
                } else {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.wallet_transferActivity5_j));
                }
            });
        });
        ClickUtil.click(ivDelete, () -> {
            etName.setText("");
        });
        setRightTitle(App.INSTANCE.getString(R.string.wallet_walletEditActivity2_j), v -> {
            if (TextUtils.isEmpty(etName.getText())) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.wallet_walletEditActivity3_j));
                return;
            }
            walletBean.setName(etName.getText().toString());
            walletBeanViewModel.insert(walletBean);
            ToastUtil.showShort(App.INSTANCE.getString(R.string.wallet_updatePwdActivity2_j));
            finish();
        });

    }

    @Override
    protected void initData() {
        walletBeanViewModel.getUsers().observe(this, walletBeans -> {
            if (walletBeans != null) {
                if (walletBeans.get(0).getCode().equals(CoinEnum.BTC.getCode())) {
                    walletBeanBtc = walletBeans.get(0);
                    walletBeanEth = walletBeans.get(1);
                } else {
                    walletBeanBtc = walletBeans.get(1);
                    walletBeanEth = walletBeans.get(0);
                }
                if (code.equals(CoinEnum.BTC.getCode())) {
                    walletBean = walletBeanBtc;
                } else {
                    walletBean = walletBeanEth;
                }
                updateUI();
            }
        });
    }

    private void updateUI() {
        etName.setText(walletBean.getName());
        if (TextUtils.isEmpty(walletBean.getWords())) {
            stvExportHelpWord.setVisibility(View.GONE);
        }
        if (TextUtils.isEmpty(walletBean.getKeystore())) {
            stvExportKeystore.setVisibility(View.GONE);
        }
        if (TextUtils.isEmpty(walletBean.getPri())) {
            stvExportPrivateKey.setVisibility(View.GONE);
        }
    }
}
